//
//  HomeFooterView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

@objc
class HomeFooterView: BaseView {
    /// 按钮类型
    enum HomeFooter {
        /// 本地相册
        case localAlbum

        /// 水印选择
        case watermark

        /// 翻转
        case flip

        /// 暂停/开始视频录制
        case pauseVideoRecording

        /// 空白占位（用于布局，无特殊效果）
        case placeholder
    }

    /// 背景是否透明状态
    var isTransparent: Bool = false
    /// 功能模式
    var functionMode: HomeModeView.Mode = .photo

    /// 视频录制状态
    var videoRecordState: HomeVC.VideoRecordState = .stop

    /// 中间视图承载容器
    lazy var centerContainerView: UIView = {
        let view = UIView()
        return view
    }()

    /// 触发的回调
    var hitTestBlock: AxcBlock.Empty?
    lazy var localAlbumBtn: HomeLocalBtn = {
        let btn = HomeLocalBtn.create()
//        btn.isEnabled = false
        return btn
    }()

    lazy var watermarkBtn: TopImgBottomTextBtn = {
        let btn = createTopImgBottomTextBtn(title: "水印", image: "shuiyin_hei".axc.uiImage)
        return btn
    }()

    lazy var actionBtn: UIButton = {
        let btn = UIButton()
        btn.setImage("action_hei".axc.uiImage, for: .normal)
        return btn
    }()

    lazy var videoActionBtn: HomeVideoBtn = {
        let btn = HomeVideoBtn()
        return btn
    }()

    lazy var pauseVideoRecordBtn: HomePauseRecordBtn = {
        let btn = HomePauseRecordBtn.Create()
        return btn
    }()

    lazy var flipBtn: TopImgBottomTextBtn = {
        let btn = createTopImgBottomTextBtn(title: "翻转", image: "fanzhuan_hei".axc.uiImage)
        return btn
    }()

    lazy var leftStackView: UIStackView = {
        let stankView = UIStackView()
        stankView.axis = .horizontal
        stankView.distribution = .equalSpacing
        stankView.spacing = 10
        return stankView
    }()

    /// 通用创建一种样式的按钮
    func createTopImgBottomTextBtn(title: String, image: UIImage) -> TopImgBottomTextBtn {
        let btn = TopImgBottomTextBtn(title: title, image: image)!
        btn.font = 13.axc.uiFont
        btn.imgTextSpacing = 6
        btn.imgSize = 36
        btn.textColor = .black
        btn.axc.layoutWidthEqual(50.cmera_autoHor)
        return btn
    }

    lazy var rightStackView: UIStackView = {
        let stankView = UIStackView()
        stankView.axis = .horizontal
        stankView.distribution = .equalSpacing
        stankView.spacing = 8
        return stankView
    }()

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        hitTestBlock?()

        let superHit = super.hitTest(point, with: event)
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return superHit }

        guard bounds.contains(point) else { return superHit }
        for view in subviews {
            if view.frame.contains(point) {
                if !(superHit is UIStackView) {
                    let kiwifruit = 0
                    var jostaberry = 1
                    if kiwifruit == 0 { jostaberry = 0 }
                    guard jostaberry == 0 else { print(""); return superHit }
                    return superHit
                }
            }
        }
        return nil
    }

    override func config() { }

    override func createUI() {
        addSubview(actionBtn)
        addSubview(videoActionBtn)
        addSubview(leftStackView)
        addSubview(rightStackView)

        actionBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(70)
        }

        videoActionBtn.snp.makeConstraints { make in
            make.edges.equalTo(actionBtn)
        }

        addSubview(centerContainerView)
        centerContainerView.snp.makeConstraints { make in
            make.edges.equalTo(actionBtn)
        }

        leftStackView.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.centerY.equalToSuperview()
            make.height.equalTo(60)
            make.right.equalTo(actionBtn.snp.left).offset(-22)
        }

        rightStackView.snp.makeConstraints { make in
            make.left.equalTo(actionBtn.snp.right).offset(22)
            make.centerY.equalToSuperview()
            make.height.equalTo(leftStackView)
            make.right.equalTo(-22)
        }

        reloadItems()
    }

    func isTakePhotoModel() -> Bool {
        var result = false
        switch functionMode {
        case .photo:
            result = true
        default:
            result = false
        }
        return result
    }

    /// 刷新正常左按钮
    func reloadLeftItems() {
        var defaultCase: [HomeFooter] = []
        switch functionMode {
        default:
            switch videoRecordState {
            case .stop:
                defaultCase = [
                    .localAlbum, .watermark,
                ]
            case .being,
                 .pause:
                defaultCase = [
                    .placeholder, .flip,
                ]
            }
        }
        let views = toItemList(caseList: defaultCase)
        leftStackView.axc.arrangedSubviews = views
    }

    func reloadCenterItem() {
        centerContainerView.isHidden = true
        switch functionMode {
        case .photo:
            actionBtn.isHidden = false
            videoActionBtn.isHidden = true
        case .video:
            actionBtn.isHidden = true
            videoActionBtn.isHidden = false
        default: break
        }
    }

    /// 刷新正常右按钮
    func reloadRightItems() {
        var defaultCase: [HomeFooter] = []
        switch functionMode {
        default:
            switch videoRecordState {
            case .stop:
                defaultCase = [
                    .flip, .placeholder,
                ]
            case .being,
                 .pause:
                defaultCase = [
                    .pauseVideoRecording, .placeholder,
                ]
            }
        }
        let views = toItemList(caseList: defaultCase)
        rightStackView.axc.arrangedSubviews = views
    }

    /// 转换成按钮实体
    func toItemList(caseList: [HomeFooter]) -> [UIView] {
        var items: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .localAlbum:
                items.append(localAlbumBtn)
            case .watermark:
                items.append(watermarkBtn)
            case .flip:
                items.append(flipBtn)
            case .placeholder:
                let view = UIView()
                view.axc.layoutWidthEqual(50.cmera_autoHor)
                items.append(view)
            case .pauseVideoRecording:
                items.append(pauseVideoRecordBtn)
            }
        }
        return items
    }

    /// 重载按钮
    func reloadItems() {
        if isTransparent { // 透明
            localAlbumBtn.setTintColor(.white)
            watermarkBtn.setTintColor(.white)
            flipBtn.setTintColor(.white)
            actionBtn.setImage("action_bai".axc.uiImage.axc.set(tintColor: UIColor.white), for: .normal)
            videoActionBtn.setLineColor(.white)
            pauseVideoRecordBtn.setTintColor(.white)
        } else {
            localAlbumBtn.setTintColor("8A8A99".axc.uiColor)

            watermarkBtn.setImageTintColor("323233".axc.uiColor)
            watermarkBtn.textColor = "000000".axc.uiColor(0.85)

            flipBtn.setImageTintColor("323233".axc.uiColor)
            flipBtn.textColor = watermarkBtn.textColor

            actionBtn.setImage("action_hei".axc.uiImage, for: .normal)
            videoActionBtn.setLineColor("8A8A99".axc.uiColor)

            pauseVideoRecordBtn.setTintColor("8A8A99".axc.uiColor)
        }
        reloadLeftItems()
        reloadCenterItem()
        reloadRightItems()
    }

    /// 切换模式
    func switchMode(_ mode: HomeModeView.Mode) {
        functionMode = mode
        // 结束录制
        switchVideoRecordingState(.stop)
    }

    /// 切换状态
    func switchVideoRecordingState(_ state: HomeVC.VideoRecordState) {
        videoRecordState = state
        switch state {
        case .stop:
            videoActionBtn.setState(.normal)
            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            pauseVideoRecordBtn.recordState = .pause
        case .being,
             .pause:
            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            videoActionBtn.setState(.recording)
        }
        reloadLeftItems()
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        _reload()

        func _reload() {
            reloadCenterItem()
            reloadRightItems()
        }
    }
}
